package com.smartcampusbackend.service.impl;

import com.smartcampusbackend.mapper.UserReadStatusMapper;
import com.smartcampusbackend.model.UserReadStatus;
import com.smartcampusbackend.service.UserReadStatusService;
import com.smartcampusbackend.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserReadStatusServiceImpl implements UserReadStatusService {
    @Autowired
    private UserReadStatusMapper userReadStatusMapper;
    
    @Autowired
    @Lazy
    private AnnouncementService announcementService;

    @Override
    public int markAsRead(Integer userId, Integer announcementId) {
        // 先检查是否存在记录，不存在则创建
        UserReadStatus existingStatus = getReadStatus(userId, announcementId);
        if (existingStatus == null) {
            UserReadStatus newStatus = new UserReadStatus();
            newStatus.setUserId(userId);
            newStatus.setAnnouncementId(announcementId);
            newStatus.setIsRead(true);
            int result = userReadStatusMapper.insertReadStatus(newStatus);
            return result;
        } else {
            int result = userReadStatusMapper.markAsRead(userId, announcementId);
            return result;
        }
    }

    @Override
    public UserReadStatus getReadStatus(Integer userId, Integer announcementId) {
        return userReadStatusMapper.selectReadStatus(userId, announcementId);
    }

    @Override
    public List<UserReadStatus> getUserReadStatus(Integer userId, String type) {
        return userReadStatusMapper.selectReadStatusByUser(userId, type);
    }

    @Override
    public int batchCreateReadStatus(Integer userId, List<Integer> announcementIds) {
        if (announcementIds == null || announcementIds.isEmpty()) {
            return 0;
        }
        return userReadStatusMapper.batchInsertReadStatus(userId, announcementIds);
    }

    @Override
    public int deleteReadStatusByAnnouncementId(Integer announcementId) {
        return userReadStatusMapper.deleteReadStatusByAnnouncementId(announcementId);
    }
    
    // 为新用户初始化阅读状态（当新用户注册时调用）
    public void initializeUserReadStatus(Integer userId) {
        // 获取所有新闻和通知的ID
        List<com.smartcampusbackend.model.Announcement> newsList = 
            announcementService.getAnnouncements("NEWS", null);
        List<com.smartcampusbackend.model.Announcement> noticeList = 
            announcementService.getAnnouncements("NOTICE", null);
        
        // 合并所有公告ID
        List<Integer> allAnnouncementIds = newsList.stream()
            .map(com.smartcampusbackend.model.Announcement::getId)
            .collect(Collectors.toList());
        allAnnouncementIds.addAll(
            noticeList.stream()
                .map(com.smartcampusbackend.model.Announcement::getId)
                .collect(Collectors.toList())
        );
        
        // 批量创建阅读状态记录
        if (!allAnnouncementIds.isEmpty()) {
            batchCreateReadStatus(userId, allAnnouncementIds);
        }
    }
    
    // 为新公告初始化所有用户的阅读状态（当新公告发布时调用）
    public void initializeAnnouncementReadStatus(Integer announcementId, List<Integer> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return;
        }
        
        for (Integer userId : userIds) {
            UserReadStatus readStatus = new UserReadStatus();
            readStatus.setUserId(userId);
            readStatus.setAnnouncementId(announcementId);
            readStatus.setIsRead(false);
            userReadStatusMapper.insertReadStatus(readStatus);
        }
    }
} 